﻿using DalatCooking.Core;
using DalatCooking.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Menu_Add_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            Person p = Session.GetPersonLogged();
            if (!p.IsInRole("SuperUser"))
                Response.Redirect("/admin/accessdeny.aspx");
        }

        ddlGroup.DataSource = MenuGroupManager.FindAll();
        ddlGroup.DataTextField = "Name";
        ddlGroup.DataValueField = "GroupId";
        ddlGroup.DataBind();
        LoadParentDropDown(ddlMenuParent, 0);
        if (MenuID != -1)
        {
            BinMenuInfo();
        }
        base.OnInit(e);
    }
    static int level = -1;
    public static void LoadParentDropDown(DropDownList ddl, int parentID)
    {
        level++;
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        List<DalatCooking.Core.Menu> list = MenuManager.FindAll().FindAll(ca => ca.ParentID == parentID && ca.IsActive == true);
        for (int i = 0; i < list.Count; i++)
        {
            ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name, list[i].MenuID.ToString()));
            LoadParentDropDown(ddl, list[i].MenuID); // Goi de quy ham              
        }
        level--;
    }
    protected string GetParentTitle(object obj)
    {
        Category temp = CategoryManager.Single(int.Parse(obj.ToString()));
        if (temp == null)
            return "";
        else
            return StringHelper.RemoveDiacriticsURL(temp.Name);
    }
    public string GetCategoryUrl(object CategoryID)
    {
        string html = "";
        html += "" + StringHelper.RemoveDiacriticsURL(CategoryManager.Single(int.Parse(CategoryID.ToString())).Name) + "-" + CategoryID.ToString();
        return html;
    }
    public string GetContentUrl(object ContentID)
    {
        string html = "";
       // html += "" + StringHelper.RemoveDiacriticsURL(Categories.Single(Contents.Single(ContentID).CategoryID).CategoryName) + "/" + Contents.Single(ContentID).Slug + "/" + ContentID.ToString();
        return html;
    }

    void BinMenuParent()
    {
        foreach (var item in MenuManager.FindAll().FindAll(c=>c.ParentID == 0))
        {
            ddlMenuParent.Items.Add(new ListItem(item.Name, item.MenuID.ToString()));
            foreach (var item1 in MenuManager.FindAll().FindAll(c => c.ParentID == 0))
            {
                ddlMenuParent.Items.Add(new ListItem("----" + item1.Name, item1.MenuID.ToString()));
            }
        }
    }
    void BinMenuInfo()
    {
        DalatCooking.Core.Menu m = MenuManager.Single(MenuID);
        if (m != null)
        {
            txtName.Text = m.Name;
            txtSortIndex.Text = m.SortIndex.ToString();
            txtUrl.Text = m.Url;
            txtIconUrl.Text = m.IconUrl;
            ddlGroup.SelectedValue = m.GroupID.ToString();
            ddlMenuParent.SelectedValue = m.ParentID.ToString();
            cbIsActive.Checked = m.IsActive;          
        }
    }
    public int MenuID
    {
        get
        {
            if (Request.QueryString["MenuID"] != null)
                return ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString["MenuID"]));
            else return -1;
        }
    }
    void Save()
    {
        DalatCooking.Core.Menu m = new DalatCooking.Core.Menu();
        m.MenuID = -1;
        m.Name = txtName.Text;
        m.Url = txtUrl.Text;
        m.IconUrl = txtIconUrl.Text;
        m.IsActive = cbIsActive.Checked;
        m.GroupID = ConvertObject.ToInt(ddlGroup.SelectedValue); ;
        m.ParentID = ConvertObject.ToInt(ddlMenuParent.SelectedValue);
        bool rs = MenuManager.Insert(m);
        if (rs)
            Response.Redirect("Default.aspx", true);

    }
    void Update()
    {
        DalatCooking.Core.Menu m = new DalatCooking.Core.Menu();
        m.MenuID = MenuID;
        m.Name = txtName.Text;
        m.Url = txtUrl.Text;
        m.IconUrl = txtIconUrl.Text;
        m.IsActive = cbIsActive.Checked;
        m.GroupID = ConvertObject.ToInt(ddlGroup.SelectedValue);
        m.ParentID = ConvertObject.ToInt(ddlMenuParent.SelectedValue);
        m.SortIndex = ConvertObject.ToInt(txtSortIndex.Text);
        bool rs = MenuManager.Update(m);
        if (rs )
            Response.Redirect("Default.aspx", true);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (MenuID != -1)
        {
            Update();
        }
        else
        {
            Save();
        }
    }
    protected void btnCategory_Click(object sender, EventArgs e)
    {
        /*
        Menus m = new Menus();
        m.UniqueName = StringHelper.RemoveWhitespace(txtCategory.Text.Trim());
        m.Name = txtCategory.Text;
        if (Menus.MenuType_Add(m) > 0)
            Response.Redirect(Request.RawUrl);
         * */
    }
}